package com.easyJava.builder;

import com.easyJava.bean.Constants;
import com.easyJava.bean.FieldInfo;
import com.easyJava.bean.TableInfo;
import com.easyJava.utils.BufferedWriterUtils;
import com.easyJava.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@Slf4j
public class BuildService {
    public static void execute(TableInfo tableInfo){
        File folder = new File(Constants.PATH_SERVICE);
        folder.mkdirs();
        String serviceName = tableInfo.getBeanName() + Constants.SUFFIX_SERVICE;

        File file = new File(folder, serviceName + Constants.FILE_SUFFIX_JAVA);

        try (
                OutputStream out = new FileOutputStream(file);
                OutputStreamWriter ow = new OutputStreamWriter(out, StandardCharsets.UTF_8);
                BufferedWriter bufferedWriter = new BufferedWriter(ow);
        ){
            BufferedWriterUtils bw = new BufferedWriterUtils(bufferedWriter);

            //生成包头
            bw.write("package " + Constants.PACKAGE_SERVICE + ";");
            bw.newLine();

            //导包
            bw.write("import " + Constants.PACKAGE_PO +  "."  + tableInfo.getBeanName() + ";");
            bw.write("import " + Constants.PACKAGE_QUERY + "." + tableInfo.getBeanQueryName() + ";");
            bw.write("import " + Constants.PACKAGE_VO + ".PaginationResultVO;");
            bw.write("import java.util.List;");
            bw.newLine();

            //生成注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + "Service接口");
            //接口
            bw.write("public interface " + serviceName + " {");

            generateBaseMethod(bw, tableInfo);

            generateExtendMethod(bw, tableInfo);


            bw.write("}");
            bw.flush();
        }catch (Exception e){
            log.error("创建service失败:{}", e);
        }
    }

    private static void generateBaseMethod(BufferedWriterUtils bw, TableInfo tableInfo) throws IOException {
        bw.newLine();
        String methodParam = tableInfo.getBeanName() + " "  + StringUtils.lowerFirstChar(tableInfo.getBeanName());
        String methodQueryParam = tableInfo.getBeanQueryName() + " "  + StringUtils.lowerFirstChar(tableInfo.getBeanQueryName());

        BuildComment.createMethodComment(bw, "新增");
        bw.write("\tint add(" + methodParam + ");");
        bw.newLine();

        BuildComment.createMethodComment(bw, "新增或修改");
        bw.write("\tint addOrUpdate(" +  methodParam + ");");
        bw.newLine();

        BuildComment.createMethodComment(bw, "批量新增");
        bw.write("\tlong addBatch(List<" + tableInfo.getBeanName() + "> list);");
        bw.newLine();

        BuildComment.createMethodComment(bw, "批量新增或修改");
        bw.write("\tlong addOrUpdateBatch(List<" + tableInfo.getBeanName() + "> list);");
        bw.newLine();

        BuildComment.createMethodComment(bw, "根据参数获取集合");
        bw.write("\tList<" + tableInfo.getBeanName() + "> getList(" + methodQueryParam + ");");
        bw.newLine();

        BuildComment.createMethodComment(bw, "根据参数查询数量");
        bw.write("\tlong getCount(" + methodQueryParam + ");");
        bw.newLine();

        BuildComment.createMethodComment(bw, "分页查询");
        bw.write("\tPaginationResultVO<" + tableInfo.getBeanName() + "> getPageList(" + methodQueryParam + ");");
        bw.newLine();
    }

    private static void generateExtendMethod(BufferedWriterUtils bw, TableInfo tableInfo) throws IOException {
        Map<String, List<FieldInfo>> indexMap = tableInfo.getIndexMap();
        for(Map.Entry<String, List<FieldInfo>> entry : indexMap.entrySet()){
            List<FieldInfo> fieldInfoList = entry.getValue();
            StringBuffer methodNameSB = new StringBuffer();
            StringBuffer paramSB = new StringBuffer();
            int index = 0;
            for(FieldInfo fieldInfo : fieldInfoList){
                index++;
                methodNameSB.append(StringUtils.upperFirstChar(fieldInfo.getBeanFieldName()));
                paramSB.append(fieldInfo.getJavaType()  + " "  + fieldInfo.getBeanFieldName());
                if(index < fieldInfoList.size()){
                    methodNameSB.append("And");
                    paramSB.append(", ");
                }
            }
            String methodName = methodNameSB.toString();
            String param = paramSB.toString();

            BuildComment.createMethodComment(bw, "根据" + methodName + "查询");
            bw.write("\t" + tableInfo.getBeanName() + " getBy" + methodName + "(" + param + ");");
            bw.newLine();

            BuildComment.createMethodComment(bw, "根据" + methodName + "修改");
            bw.write("\tint" + " updateBy" + methodName + "(" + tableInfo.getBeanName() + " " + StringUtils.lowerFirstChar(tableInfo.getBeanName()) + ", " + param + ");");
            bw.newLine();

            BuildComment.createMethodComment(bw, "根据" + methodName + "删除");
            bw.write("\tint" + " deleteBy" + methodName + "(" + param + ");");
            bw.newLine();
        }
    }
}
